<?php

namespace mip;

use think\Cache;
use think\Log;
use think\Request;
use think\Session;
use think\Response;

use mip\Init;
class AuthApiBase extends ApiBase
{
    
    public static $accessTokenPrefix = 'accessToken_';

    public function _initialize() {
        parent::_initialize();


    }

    /**
     *
     * @param $accessToken
     * @param $terminal
     * @param $uid
     * @return bool|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _auth($accessToken,$terminal,$uid) {
        $accessTokenInfo = $this->_getAccessTokenInfo($accessToken,$terminal,$uid);
        if (!$accessTokenInfo) {
            return false;
        }
        return $accessTokenInfo;
    }

    /**
     * 将
     * @param $accessToken
     * @param $terminal
     * @param $group_id
     * @return bool|mixed
     */
    protected function _getAccessTokenInfo($accessToken,$terminal,$u_id)
    {
        $keys = self::$accessTokenPrefix . $terminal .'_'. $accessToken .'_'. $u_id;
        $info = Cache::get($keys);
        if ($info == false || $info['expires_time'] < time()) return false;

        self::_saveAccessToken($accessToken,$terminal,$u_id,$info );
        return $info;
    }
    
    public static function saveAccessToken($accessToken,$terminal,$uid, $accessTokenInfo ) {
        $expires = 172800;
        Cache::set(self::$accessTokenPrefix . $terminal .'_'. $accessToken .'_'. $uid, $accessTokenInfo, $expires);
    }

}